/**
 * Created by pankajmishra on 26/01/15.
 */
define(['jquery', 'mustache','module/template/homeTemplate'], function($, mustache, template){


    function render(obj){
        var dynaClass = 'js_peopler', mainUpdate = $('.main-content'),
            loading = $(template['home-loading']);
        mainUpdate.empty();
        mainUpdate.addClass(dynaClass);
        mainUpdate.append(loading);


        $.getJSON(obj.url).done(function(response){
            response = process(response);

            if(response.length == 0){
                $('.js_peopler').append($( template['people-search-error']));
                loading.remove();
                return;
            }
            $.each(response, function(index, user){
                $('.js_peopler').append($( mustache.render(template['people-search'], user)));
                loading.remove();
            });
        });
    };

    function process(response){
        response = $.isArray(response) ? response :[response] ;
        $.each(response, function(index,item){
            item.livingStatus = 'Owner';
            if(item.renter === 1){
                item.livingStatus = 'Renter';
            }
            if(!item.profession){
                item.profession = '--'
            }

            var item1 = item;
            if(item.groups){
                $.each(item.groups, function(index,item){
                    if(item.type == 'block'){
                        item1.blockName = item.desc;
                    }
                });
            }
        });
        return response;
    }

    return {
        render : render,
        processUserResult : process
    };
});